package www.com.guigu.ljq;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class Loginljq implements HandlerInterceptor {
    //Controlle执行前调用此方法
    //返回true表示继续执行，返回false终止
    //这里可以加入登录校验。权限拦截等
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session=request.getSession();
        //获取请求的地址
        String requestURI = request.getRequestURI();
        System.out.println(requestURI);
        if (requestURI.contains("ss")){
         return true;
        }
        if(session.getAttribute("user")!=null){
          return  true;
        }
        response.sendRedirect("index.jsp");
        return false;
    }
    //controller执行后但未返回试图前调用此方法
    //这里可以在返回用户前对模型数据进行加工处理
    //比如这里加入公用信息以便页面显示
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }
    //controller执行后且视图返回后调用此方法
    //这里可得到执行controller时的异常信息
    //这里可以记录操作日志
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
